﻿using System;
using System.Collections.Generic;
using System.Linq;
using MyAMS.WebServer.Models;

namespace MyAMS.WebServer.Repositories
{
    class NodeRepository : INodeRepository
    {
        private List<Node> nodes;

        public NodeRepository()
        {
            string[] s = new string[] { "aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg", "hhh", "iii", "jjj", "kkk", "lll", "mmm", "nnn", "ooo", "ppp", "qqq", "rrr", "sss", "ttt" };

            nodes = new List<Node>();
            for (int i = 0; i < 50; i++)
            {
                Node n = new Node();
                n.RoutingRoles = new List<RoutingRole>();
                n.RoutingRoles.Add(new RoutingRole { Area = "Node", ID = new Guid(10 * i, 1, 1, new byte[] { 1, 1, 1, 1, 1, 1, 1, 1 }), InheritanceType = RoutingRoleInheritance.None, Uri = "{Name}" });
                n.Body = i.ToString();
                n.BodyShort = "摘要" + i.ToString();
                List<BaseNode> chNodes = new List<BaseNode>();
                for (int j = 0; j < 10; j++)
                {
                    Node m = new Node();
                    m.RoutingRoles = new List<RoutingRole>();
                    m.RoutingRoles.Add(new RoutingRole { Area = "Node", ID = new Guid(11 * i, 1, 1, new byte[] { 1, 1, 1, 1, 1, 1, 1, 1 }), InheritanceType = RoutingRoleInheritance.None, Uri = "{Name}" });
                    m.RoutingRoles.Add(new RoutingRole { Area = "Node", ID = new Guid(12 * i, 1, 1, new byte[] { 1, 1, 1, 1, 1, 1, 1, 1 }), InheritanceType = RoutingRoleInheritance.None, Uri = s[j] });
                    m.Body = i.ToString() + "-" + j.ToString();
                    m.BodyShort = "摘要" + i.ToString() + "-" + j.ToString();
                    if (j == 1)
                    {
                        m.Name = "LB";
                    }
                    else
                    {
                        m.Name = s[j];
                    }

                    m.Parent = n;
                    m.MaximumChildren = 10;
                    m.MaximumLevel = 1;
                    m.ID = new Guid(100 * (i + 1) + j, 1, 1, new byte[] { 1, 1, 1, 1, 1, 1, 1, 1 });
                    m.Parent = n;
                    m.Domains = new List<string>();
                    chNodes.Add(m);
                    nodes.Add(m);
                }
                n.Children = chNodes;
                n.Created = DateTime.Now;
                n.IsOwned = true;
                List<string> nodedomain = new List<string>();
                if (i == 1)
                {
                    nodedomain.Add("localhost");
                }
                nodedomain.Add("localhost" + i.ToString());
                n.Domains = nodedomain;
                n.ID = new Guid(i, 1, 1, new byte[] { 1, 1, 1, 1, 1, 1, 1, 1 });
                n.Keywords = new List<string>();
                n.MaximumChildren = 100;
                n.MaximumLevel = 5;
                n.Name = "";
                nodes.Add(n);
            }
        }

        #region IRepository<Node> Members

        public IEnumerable<Node> Get()
        {
            return nodes.AsQueryable();
        }

        public Node Get(Guid ID)
        {
            return nodes.SingleOrDefault<Node>(n => n.ID == ID);
        }

        #endregion

        public Node Get(string domainName)
        {
            return nodes.SingleOrDefault<Node>(n => n.Domains.Contains(domainName));
        }
    }
}
